<?php
	namespace LEA\StageBundle\Utility;

	use Symfony\Component\HttpFoundation\Session;

	class Utility {
	        function constructAllGroupesKeys()
    {
        $keys=array("M1MIAGEFA,M2MIAGEFA,M1INFOFA,M2IAGLFA,"
                               ."M2ESERVFA,M2TIIRFA,M2IVIFA,M2MOCADFA",
                               "M1MIAGEFA,M2MIAGEFA",
                               "M1MIAGEFA",
                               "M2MIAGEFA",
                               "M1INFOFA,M2IAGLFA,M2ESERVFA,M2TIIRFA,M2IVIFA,M2MOCADFA",
                               "M1INFOFA",
                               "M2IAGLFA,M2ESERVFA,M2TIIRFA,M2IVIFA,M2MOCADFA",
                               "M2IAGLFA",
                               "M2ESERVFA",
                               "M2TIIRFA",
                               "M2IVIFA",
                               "M2MOCADFA");
        $values=array("Toutes","MIAGE FA","M1 MIAGE FA","M2 MIAGE FA","INFO FA","M1 INFO FA","M2 INFO FA","M2 IAGL FA","M2 E-SERV FA","M2 TIIR FA", "M2 IVI FA","M2 MOCAD FA" );
        $result=array("keys"=>$keys,"values"=>$values);
        return $result;
    }

    function initPredefinedFIKeysValues($onlyM1INFO=false) {

            $predefinedKeys["ALL_RESP_FI"]="M1MIAGEFI,M2MIAGEFI,M1INFOFI,M2IAGLFI,"
                               ."M2ESERVFI,M2TIIRFI,M2IVIFI,M2MOCADFI";
            $predefinedValues["ALL_RESP_FI"]="MIAGE &amp; INFO FI";

            $predefinedKeys["L3MIAGEFI_RESP"]="L3MIAGEFI";
            $predefinedValues["L3MIAGEFI_RESP"]=" - MIAGE - L3 FI";


            $predefinedKeys["L3INFOFI_RESP"]="L3INFOFI";
            $predefinedValues["L3INFOFI_RESP"]=" - INFO - L3 FI";


            $predefinedKeys["MMIAGEFI_RESP"]="M1MIAGEFI,M2MIAGEFI";
            $predefinedValues["MMIAGEFI_RESP"]="MIAGE - M1 &amp; M2 FI";

            $predefinedKeys["M1MIAGEFI_RESP"]="M1MIAGEFI";
            $predefinedValues["M1MIAGEFI_RESP"]=" - MIAGE - M1 FI";

            $predefinedKeys["M2MIAGEFI_RESP"]="M2MIAGEFI";
            $predefinedValues["M2MIAGEFI_RESP"]=" - MIAGE - M2 FI";

            if ($onlyM1INFO==false)  {
              $predefinedKeys["MINFOFI_RESP"]="M1INFOFI,M2IAGLFI,M2ESERVFI,M2TIIRFI,M2IVIFI,M2MOCADFI";
              $predefinedValues["MINFOFI_RESP"]="INFO - M1 &amp; M2 FI";
            }

            $predefinedKeys["M1INFOFI_RESP"]="M1INFOFI,M1IAGLFI,M1ESERVFI,M1TIIRFI,M1IVIFI,M1MOCADFI";
            $predefinedValues["M1INFOFI_RESP"]="INFO - M1 FI";

            if ($onlyM1INFO==false) {
              $predefinedKeys["M1ESERVFI_RESP"]="M1ESERVFI";
              $predefinedValues["M1ESERVFI_RESP"]=" - ESERV - M1 FI";

              $predefinedKeys["M1IAGLFI_RESP"]="M1IAGLFI";
              $predefinedValues["M1IAGLFI_RESP"]=" - IAGL - M1 FI";

              $predefinedKeys["M1IVIFI_RESP"]="M1IVIFI";
              $predefinedValues["M1IVIFI_RESP"]=" - IVI - M1 FI";

              $predefinedKeys["M1MOCADFI_RESP"]="M1MOCADFI";
              $predefinedValues["M1MOCADFI_RESP"]=" - MOCAD - M1 FI";

              $predefinedKeys["M1TIIRFI_RESP"]="M1TIIRFI";
              $predefinedValues["M1TIIRFI_RESP"]=" - TIIR - M1 FI";

              $predefinedKeys["M1INFOFIG_RESP"]="M1INFOFI";
              $predefinedValues["M1INFOFIG_RESP"]=" - Sans Spec - M1 FI";
            }
            $predefinedKeys["M2INFOFI_RESP"]="M2IAGLFI,M2ESERVFI,M2TIIRFI,M2IVIFI,M2MOCADFI";
            $predefinedValues["M2INFOFI_RESP"]="INFO - M2 FI";

            $predefinedKeys["M2ESERVFI_RESP"]="M2ESERVFI";
            $predefinedValues["M2ESERVFI_RESP"]=" - ESERV - M2 FI";

            $predefinedKeys["M2IAGLFI_RESP"]="M2IAGLFI";
            $predefinedValues["M2IAGLFI_RESP"]=" - IAGL - M2 FI";

            $predefinedKeys["M2IVIFI_RESP"]="M2IVIFI";
            $predefinedValues["M2IVIFI_RESP"]=" - IVI - M2 FI";

            $predefinedKeys["M2MOCADFI_RESP"]="M2MOCADFI";
            $predefinedValues["M2MOCADFI_RESP"]=" - MOCAD - M2 FI";

            $predefinedKeys["M2TIIRFI_RESP"]="M2TIIRFI";
            $predefinedValues["M2TIIRFI_RESP"]=" - TIIR - M2 FI";




            return array("keys"=>$predefinedKeys,"values"=>$predefinedValues);
    }


function initPredefinedFAKeysValues($onlyM1INFO=false) {


            $predefinedKeys["ALL_RESP_FA"]="M1MIAGEFA,M2MIAGEFA,M1INFOFA,M2IAGLFA,"
                               ."M2ESERVFA,M2TIIRFA,M2IVIFA,M2MOCADFA";
            $predefinedValues["ALL_RESP_FA"]="MIAGE &amp; INFO FA";

            $predefinedKeys["L3MIAGEFA_RESP"]="L3MIAGEFA";
            $predefinedValues["L3MIAGEFA_RESP"]=" - MIAGE - L3 FA";


            $predefinedKeys["L3INFOFA_RESP"]="L3INFOFA";
            $predefinedValues["L3INFOFA_RESP"]=" - INFO - L3 FA";


            $predefinedKeys["MMIAGEFA_RESP"]="M1MIAGEFA,M2MIAGEFA";
            $predefinedValues["MMIAGEFA_RESP"]="MIAGE - M1 &amp; M2 FA";

            $predefinedKeys["M1MIAGEFA_RESP"]="M1MIAGEFA";
            $predefinedValues["M1MIAGEFA_RESP"]=" - MIAGE - M1 FA";

            $predefinedKeys["M2MIAGEFA_RESP"]="M2MIAGEFA";
            $predefinedValues["M2MIAGEFA_RESP"]=" - MIAGE - M2 FA";

            if ($onlyM1INFO==false)  {
                $predefinedKeys["MINFOFA_RESP"]="M1INFOFA,M2IAGLFA,M2ESERVFA,M2TIIRFA,M2IVIFA,M2MOCADFA";
                $predefinedValues["MINFOFA_RESP"]="INFO - M1 &amp; M2 FA";
            }

            $predefinedKeys["M1INFOFA_RESP"]="M1INFOFA,M1IAGLFA,M1ESERVFA,M1TIIRFA,M1IVIFA,M1MOCADFA";
            $predefinedValues["M1INFOFA_RESP"]="INFO - M1 FA";

            if ($onlyM1INFO==false) {
              $predefinedKeys["M1ESERVFA_RESP"]="M1ESERVFA";

              $predefinedValues["M1ESERVFA_RESP"]=" - ESERV - M1 FA";

              $predefinedKeys["M1IAGLFA_RESP"]="M1IAGLFA";
              $predefinedValues["M1IAGLFA_RESP"]=" - IAGL - M1 FA";

              $predefinedKeys["M1IVIFA_RESP"]="M1IVIFA";
              $predefinedValues["M1IVIFA_RESP"]=" - IVI - M1 FA";

              $predefinedKeys["M1MOCADFA_RESP"]="M1MOCADFA";
              $predefinedValues["M1MOCADFA_RESP"]=" - MOCAD - M1 FA";

              $predefinedKeys["M1TIIRFA_RESP"]="M1TIIRFA";
              $predefinedValues["M1TIIRFA_RESP"]=" - TIIR - M1 FA";

              $predefinedKeys["M1INFOFAG_RESP"]="M1INFOFA";
              $predefinedValues["M1INFOFAG_RESP"]=" - Sans Spec - M1 FA";
            }

            $predefinedKeys["M2INFOFA_RESP"]="M2IAGLFA,M2ESERVFA,M2TIIRFA,M2IVIFA,M2MOCADFA";
            $predefinedValues["M2INFOFA_RESP"]="INFO - M2 FA";

            $predefinedKeys["M2ESERVFA_RESP"]="M2ESERVFA";
            $predefinedValues["M2ESERVFA_RESP"]=" - ESERV - M2 FA";

            $predefinedKeys["M2IAGLFA_RESP"]="M2IAGLFA";
            $predefinedValues["M2IAGLFA_RESP"]=" - IAGL - M2 FA";

            $predefinedKeys["M2IVIFA_RESP"]="M2IVIFA";
            $predefinedValues["M2IVIFA_RESP"]=" - IVI - M2 FA";

            $predefinedKeys["M2MOCADFA_RESP"]="M2MOCADFA";
            $predefinedValues["M2MOCADFA_RESP"]=" - MOCAD - M2 FA";

            $predefinedKeys["M2TIIRFA_RESP"]="M2TIIRFA";
            $predefinedValues["M2TIIRFA_RESP"]=" - TIIR - M2 FA";




            return array("keys"=>$predefinedKeys,"values"=>$predefinedValues);

    }

    function constructGrantedGroupesKeys($unique=false,$all=false,$ft=null,$onlyM1INFO=false)
    {
        //error_log("constructing with unique:$unique all:$all ft:$ft");
        if ($ft==null)
          $ft=isset($_SESSION[REF_FORMATIONTYPE])?$_SESSION[REF_FORMATIONTYPE]:"FA/FI";

        $roles = getParam(CK_ROLES,$_SESSION[CK_ROLES]);

        /*if (strpos($roles,"ALL_RESP",0)) {
            return constructAllGroupesKeys();
        }
        else */
      {

            $predefKeys=array();
            $predefValues=array();



            //error_log("formationType ref is ".$ft);
            if (strstr($ft,"FA")!=FALSE) {
              $res=initPredefinedFAKeysValues($onlyM1INFO);
              $keys=$res["keys"];$values=$res["values"];
              $ikeys=array_keys($keys);
              for ($i=0;$i<count($ikeys);$i++) {
                $predefKeys[$ikeys[$i]]=$keys[$ikeys[$i]];
                $predefValues[$ikeys[$i]]=$values[$ikeys[$i]];
              }
            }
            if (strstr($ft,"FI")!=FALSE) {
              $res=initPredefinedFIKeysValues($onlyM1INFO);
              $keys=$res["keys"];$values=$res["values"];
              $ikeys=array_keys($keys);
              for ($i=0;$i<count($ikeys);$i++) {
                $predefKeys[$ikeys[$i]]=$keys[$ikeys[$i]];
                $predefValues[$ikeys[$i]]=$values[$ikeys[$i]];
              }
            }


            $roles = preg_replace("/PROF/","",$roles);
            $roles = preg_replace("/ETUD/","",$roles);
            $roles = preg_replace("/_SECR/","_RESP",$roles);



            //$roles = preg_replace("/_RESP/","",$roles);
            //error_log("total nb of authorizations : ".count($predefKeys));
            $index=array_keys($predefKeys);

            $keys=array();
            $values=array();

            for ($i=1,$k=1;$i<count($index);$i++) {
                $pos = strpos($roles,$index[$i],0);

//                error_log("checking for ".$index[$i]);
//                error_log($unique." ".strpos($predefKeys[$index[$i]],',')." ".$index[$i]);
//                error_log($predefValues[$index[$i]]);
//                error_log("cond 1 :".(($all==true)||($pos!==FALSE)) );
//                error_log("cond 2 : ".(!$unique || (strpos($predefKeys[$index[$i]],',')==FALSE)));
//                error_log("cond 3 : ".((strpos($predefKeys[$index[$i]],'1INFO')==1)?1:0));
//
                if (
                  (($all==true)||($pos!==FALSE)) &&
                  ((!$unique || (strpos($predefKeys[$index[$i]],',')==FALSE)) ||
                    ($onlyM1INFO && ((strpos($predefKeys[$index[$i]],'1INFO'))==1)))
                )
                {
                    $keys[$k]=$predefKeys[$index[$i]];
                    $values[$k]=$predefValues[$index[$i]];
                    $k++;
                }
            }

            if ($k>1&&!$unique) {
                $keys[0]=$keys[1];
                $values[0]="Toutes";
                for ($i=2;$i<$k;$i++)
                    $keys[0] .= ",".$keys[$i];
            } else {
              $keys[0]="";$values[0]="Aucune";
            }
        }
        $result=array("keys"=>$keys,"values"=>$values);
        //error_log ($result["values"][0]);
        //error_log ($result["keys"][0]);
        return $result;
    }

	function getListActionRespAction(){
	return array("Maj fiche étudiant", "Maj mail ULille1", "Maj mission", "Maj mission Etud", "Maj CR 1ere visite en entr.",
	"1ere visite prévue", "Maj mission soutenance", "Missions soutenance discutée", "Maj CR 2eme visite en entr.", "2eme visite prévue",
	"Envoie mail prise de contact e.-t.", "Enlever tuteur", "Migrer/Changer formation étud.", "Suppr. étud. de l'application" );
	}
}
?>